package com.meituan.controller;

import com.meituan.model.GoodsClassifyEntity;
import com.meituan.service.GoodsClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsClassifyController {
    @Autowired
    GoodsClassifyService goodsClassifyService;
    @GetMapping("getClassify")
    public Map getClassify(String shopId){
        Map map = new HashMap();
        List<GoodsClassifyEntity> list = goodsClassifyService.findByShopId(shopId);
        if(list.size()!=0){
            map.put("err_code",0);
            map.put("result",list);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getClassifyWithGoods")
    public Map getClassifyWithGoods(String shopId){
        Map map = new HashMap();
        List<GoodsClassifyEntity> list = goodsClassifyService.findByShopId(shopId);
        if(list.size()!=0){
            map.put("err_code",0);
            map.put("result",list);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("addClassify")
    public Map addClassify(@RequestBody GoodsClassifyEntity classify){
        Map map = new HashMap();
        if(goodsClassifyService.add(classify)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getClassifyById")
    public Map getClassifyById(String classifyId){
        Map map = new HashMap();
        GoodsClassifyEntity result = goodsClassifyService.findById(classifyId);
        if(result!=null){
            map.put("err_code",0);
            map.put("result",result);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("delClassify")
    public Map delClassify(String classifyId){
        Map map = new HashMap();
        if(goodsClassifyService.delete(classifyId)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }


    @GetMapping("getClassifyByShopId")
    public List<GoodsClassifyEntity> getClassifyByShopId(String shopId){
        List<GoodsClassifyEntity> list = goodsClassifyService.findByShopId(shopId);
        return list;
    }

    @PostMapping("updateClassifyName")
    public Map<String,Integer> updateClassifyName(@RequestBody LinkedHashMap data){
//        System.out.println(goodsClassifyEntity);
        Integer classifyId = (Integer) data.get("classifyId");
        String classifyName = (String) data.get("classifyName");
        Map map = new HashMap();
        if(goodsClassifyService.updateClassifyName(classifyName,String.valueOf(classifyId))==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }






}
